// store/index.js
import Vue from 'vue'
import Vuex from 'vuex'
 
import WS from '@/common/websocket';

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		isLogin: false,
		role: 'school', // 用户身份： user-普通用户, school-学校, enterprise-企业, admin-淘学
		userInfo: {},

		chatList: [],
		unReadNum: 0
	},
	mutations: {
		storeLogin(state, userInfo) { // 登录
			state.isLogin = true;
			state.userInfo = userInfo
			state.userInfo.full_address = `${userInfo.province}${userInfo.city}${userInfo.area}`
			// switch (userInfo.usertype) {
			// 	case "1":
			// 		state.role = 'user';
			// 		break
			// 	case "2":
			// 		state.role = 'school';
			// 		break
			// 	case "3":
			// 		state.role = 'enterprise';
			// 		break
			// 	case "4":
			// 		state.role = 'admin';
			// 		break
			// 	default:
			// 		state.role = 'user'
			// }
			uni.setStorageSync('userInfo', userInfo)


		},
		storeLogout(state) { // 退出登录
			state.isLogin = false
			state.userInfo = {}
			uni.clearStorageSync()
			if (getApp().globalData.ws_global) {
				getApp().globalData.ws_global.close()
			}
		}
	},
	actions: {
		connectedWS(store) {
			getApp().globalData.ws_global = new WS({
				data: {
					userId: store.state.userInfo.id
				},
				onConnected: () => {
				 
				},
				onMessage: (data) => {
					 
				}
			})

		},
 

	}
})

export default store
